package demo1

import "fmt"

type SDCard interface {
	readSD() string
	writeSD(string) int
}

type PlainSDCard struct {
}

func (p PlainSDCard) readSD() string {
	return "hello,world"
}

func (p PlainSDCard) writeSD(content string) int {
	fmt.Printf("write msg[%s] to sd card", content)
	return len(content)
}

type Computer interface {
	readSD(SDCard)
}

type Mac struct {
}

func (m Mac) readSD(sdCard SDCard) {
	content := sdCard.readSD()
	fmt.Printf("Mac read [%s]\n", content)
}

type TFCard interface {
	readTF() string
	writeTF(string) int
}

type PlainTFCard struct {
}

func (p PlainTFCard) readTF() string {
	return "Hello, go"
}

func (p PlainTFCard) writeTF(content string) int {
	fmt.Printf("write msg[%s] to tf card\n", content)
	return len(content)
}

// 适配器实现了其中一个对象的接口， 并对另一个对象进行封装。 所有流行的编程语言都可以实现适配器
type SDCardAdapterTF struct {
	tfcard TFCard
}

func (S SDCardAdapterTF) readSD() string {
	return S.tfcard.readTF()
}

func (S SDCardAdapterTF) writeSD(content string) int {
	S.tfcard.writeTF(content)
	return len(content)
}
